/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package RepositoryHarvester;

import java.sql.Timestamp;
import java.util.Date;

/**
 *
 * @author Lawrence
 */
public class HarvestRepository {

    public static void main(String [] args)
    {
        HarvestRepository hr = new HarvestRepository();
        System.setProperty("proxyPort","3128");
        System.setProperty("proxyHost","localhost");
        hr.doHarvest();
    }

    public static void doHarvest()
    {
        HarvestConfiguration thisHarvestConfig = new HarvestConfiguration(); // create a harvest configuration for this harvest

        /* decide whether to run the harvest */
        if(thisHarvestConfig.isLoaded)
        {            
            String lastDateString = thisHarvestConfig.getDateFrom();
            long harvestInterval = thisHarvestConfig.getHarvestInterval(); // get the required update interval the source            

            Timestamp lastDate = null;
            if (lastDateString != null)
                    lastDate = Timestamp.valueOf(lastDateString); // get the date/time of the last update
            else lastDate = new Timestamp (0);
            Timestamp currentDate = new Timestamp( ( new Date() ).getTime() );

            if (currentDate.getTime() - lastDate.getTime() > harvestInterval)
            { // if the harvest should be done
                /* we now start with a harvest */
                System.out.println("Doing a harvest");

                try
                {
                    /* creating the request to server */
                    ServerRequest request = new ServerRequest(thisHarvestConfig);

                    thisHarvestConfig.updateDateFrom(); // update the dateFrom in the harvest file
                    System.out.println("Harvest completed. \n");
                }
                catch(Exception e)
                {                    
                    System.err.println("Error caught in HarvestRepository.java : "+e);
                }                
            }
            else
            {
                System.out.println("Skipping harvest: Harvest interval has not yet elapsed");
            }
        }
        else
        {
              System.out.println("Aborting harvest!");
        }
    }

}
